package cn.tedu.ivos.repair.service.impl;

import cn.tedu.ivos.repair.mapper.RepairMapper;
import cn.tedu.ivos.repair.pojo.dto.RepairQuery;
import cn.tedu.ivos.repair.pojo.dto.RepairSaveParam;
import cn.tedu.ivos.repair.pojo.entity.Repair;
import cn.tedu.ivos.repair.pojo.vo.RepairVO;
import cn.tedu.ivos.repair.service.RepairService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RepairServiceImpl implements RepairService {
    @Autowired
    RepairMapper repairMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<RepairVO> selectRepair(RepairQuery repairQuery) {
        log.debug("开始处理【查询维修单】的业务，参数：{}", repairQuery);
        VehicleQuery vehicleQuery = new VehicleQuery();
        if (repairQuery.getLicense() != null){
            vehicleQuery.setLicense(repairQuery.getLicense());
            List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
            if (list.size()>0){
                List<Long> vehicleIdList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++){
                    VehicleVO vehicleVO = list.get(i);
                    Long id = vehicleVO.getId();
                    vehicleIdList.add(id);
                }
                log.debug("vehicleid的集合{}",vehicleIdList);
                repairQuery.setVehicleId(vehicleIdList);
            }
        }
        log.debug("查询到的id{}",repairQuery.getId());
        List<RepairVO> repairList = repairMapper.select(repairQuery);
        return repairList;
    }

    @Override
    public void saveRepair(RepairSaveParam repairSaveParam) {
        log.debug("开始处理【保存维修单】的业务，参数：{}", repairSaveParam);
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairSaveParam,repair);
        if (repairSaveParam.getId() == null){
            repair.setCreateTime(new Date());
            if ("1".equals(repair.getStatus())){
                repair.setStatus("3");
            }else {
                repair.setStatus("1");
            }
            log.debug("新增维修单，参数：{}", repair);
            repairMapper.insert(repair);
        }else {
            repair.setUpdateTime(new Date());
            if ("1".equals(repair.getStatus())){
                repair.setStatus("3");
            }else {
                repair.setStatus("1");
            }
            log.debug("修改维修单，参数：{}", repair);
            repairMapper.update(repair);
        }
    }

    @Override
    public void deleteRepair(Long id) {
        log.debug("开始处理【删除维修单】的业务，参数：{}", id);
        repairMapper.delete(id);
    }
}
